package com.freak.wanandroid.model.home.adapter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.wanandroid.R;
import com.freak.wanandroid.bean.SystemClassChildrenEntity;
import com.freak.wanandroid.model.home.entity.SpecialEntity;
import com.freak.wanandroid.model.home.entity.SpecialSubscriptionListAddedEntity;
import com.freak.wanandroid.model.home.entity.SpecialSubscriptionListHeadEntity;
import com.freak.wanandroid.model.home.entity.SpecialSubscriptionListTitleEntity;

import java.util.List;

/**
 * @author: freak
 * @date: 2022/9/21
 */
public class SpecialSubscriptionListAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_TITLE = 1;

    public static final int TYPE_SPECIAL_ADDED = 2;

    public static final int TYPE_SPECIAL = 3;

    public static final int TYPE_SPECIAL_HEAD = 4;

    private AppCompatActivity mAppCompatActivity;

    private OnChildrenClickListener mOnChildrenClickListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public SpecialSubscriptionListAdapter(List<MultiItemEntity> data, AppCompatActivity context) {
        super(data);
        mAppCompatActivity = context;
        addItemType(TYPE_TITLE, R.layout.item_special_subscription_list_title);
        addItemType(TYPE_SPECIAL_ADDED, R.layout.item_special_subscription_list_special_added);
        addItemType(TYPE_SPECIAL, R.layout.item_special_subscription_list_special);
        addItemType(TYPE_SPECIAL_HEAD, R.layout.item_special_subscription_list_sprcial_head);
    }

    public void setOnChildrenClickListener(OnChildrenClickListener onChildrenClickListener) {
        mOnChildrenClickListener = onChildrenClickListener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_TITLE:
                SpecialSubscriptionListTitleEntity specialSubscriptionListTitleEntity = (SpecialSubscriptionListTitleEntity) item;
                helper.setText(R.id.textViewSpecialListTitle, specialSubscriptionListTitleEntity.getTitleName());
                break;
            case TYPE_SPECIAL_ADDED:
                SpecialSubscriptionListAddedEntity systemClassChildrenEntity = (SpecialSubscriptionListAddedEntity) item;
                RecyclerView recyclerView = helper.getView(R.id.recycleViewSpecialSubscriptionAdded);
                SpecialSubscriptionAddedAdapter specialSubscriptionAddedAdapter =
                        new SpecialSubscriptionAddedAdapter(R.layout.item_special_subscription_added,
                                systemClassChildrenEntity.getEntityList());
                recyclerView.setLayoutManager(new GridLayoutManager(mAppCompatActivity, 2));
                specialSubscriptionAddedAdapter.bindToRecyclerView(recyclerView);
                specialSubscriptionAddedAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                    SystemClassChildrenEntity systemClassChildren = (SystemClassChildrenEntity) adapter.getData().get(position);
                    if (mOnChildrenClickListener != null) {
                        mOnChildrenClickListener.onChildrenClickAddOrSub(0,TYPE_SPECIAL_ADDED, systemClassChildren);
                    }
                });
                recyclerView.setAdapter(specialSubscriptionAddedAdapter);
                break;
            case TYPE_SPECIAL:
                SpecialEntity specialEntity = (SpecialEntity) item;
                RecyclerView recycleViewSpecial = helper.getView(R.id.recycleViewSpecial);
                SpecialAdapter specialAdapter = new SpecialAdapter(R.layout.item_special_list, specialEntity.getSystemClassifyBeans());
                recycleViewSpecial.setLayoutManager(new GridLayoutManager(mAppCompatActivity, 2));
                specialAdapter.bindToRecyclerView(recycleViewSpecial);
                specialAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                    SystemClassChildrenEntity classChildren = (SystemClassChildrenEntity) adapter.getData().get(position);
                    if (mOnChildrenClickListener != null) {
                        mOnChildrenClickListener.onChildrenClickAddOrSub(1,TYPE_SPECIAL, classChildren);
                    }
                });
                recycleViewSpecial.setAdapter(specialAdapter);
                break;
            case TYPE_SPECIAL_HEAD:
                SpecialSubscriptionListHeadEntity specialSubscriptionListHeadEntity = (SpecialSubscriptionListHeadEntity) item;
                helper.setText(R.id.textViewSpecialName, specialSubscriptionListHeadEntity.getSystemClassifyBean().getName());
                break;
            default:
                break;
        }
    }

    public interface OnChildrenClickListener {
        void onChildrenClickAddOrSub(int type, int itemType, SystemClassChildrenEntity systemClassChildrenEntity);
    }
}
